<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar @click-left="$router.back()" class="page-nav-bar" title="登录">
        <van-icon name="cross" slot="left" />
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="form" :show-error="false">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        maxlength="11"
        :rules="formRules.mobile"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        type="text"
        v-model="user.code"
        name="code"
        maxlength="6"
        placeholder="请输入验证码"
        :rules="formRules.code"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <!-- 右侧的内容 -->
        <template #button>
          <!-- 倒计时 -->
          <van-button v-if="isCountDownShow" class="send-sms-btn" round native-type="button" size="small" type="default">
            <van-count-down 
              :time="time"
              @finish="isCountDownShow=false"
              format='ss 秒'
            />
          </van-button>
          <!-- 发送按钮 -->
          <van-button 
            v-else 
            class="send-sms-btn" 
            round 
            @click="onSendSms"
            native-type="button" size="small" type="default">发送验证码</van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info"  native-type="submit">
          登录
        </van-button>
      </div>
    </van-form>
    <!-- /登录表单 -->
  </div>
</template>

<script>
// 导入请求方法
import { userLogin,getPhoneSms } from '@/api/user'
export default {
  name: 'LoginIndex',
  components: {},
  props: {},
  data () {
    return {
      // 1、表单的数据
      user:{
        mobile:'15375321751',
        code:'246810'
      },
      time:1000*60,  // 倒计时长
      isCountDownShow:false,  // 倒计时空间是否展示
      // 表单验证对象
      formRules:{
        mobile:[
          {required:true,message:'请输入手机号'},
          {pattern:/^1[3|4|5|6|7|8|9]\d{9}$/,message:'手机格式不正确'}
        ],
        code:[
          {required:true,message:'请输入验证码'},
          {pattern:/^\d{6}$/,message:'验证码格式不正确'}
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    // 表单提交数据
    async onSubmit (values) { // values 是表单的数据
      // 2、表单验证
      // 3、获取表单数据
      const user = this.user 
      try {
        this.$toast.loading({
          message: '加载中...',  // 提示文字
          forbidClick: true,  // 防止背景点击
          duration:0   // 0表示一直存在，后面再出现弹框就会把这个关闭！
        });
        // 4、发送请求
        const { data:res } =  await userLogin(user)
        // 5、处理请求结果
        //  弹框展示
        this.$toast.success('登录成功')
        // 将信息存储到vuex和本地存储
        this.$store.commit('SET_USER',res.data)
        // 跳转返回
        this.$router.back()
      } catch (error) {
        // 登录出错
        if(error.response.status===400){
          this.$toast.fail('验证码错误')
        }else{
          this.$toast.fail('登录出错，稍后重试')
          console.log(error);
        }
      }
    },
    // 发送验证码
    async onSendSms(){
      // 验证手机号是否符合规则 【手动自己判断某一个字段是否符合校验规则】
      try {
        await this.$refs.form.validate('mobile')
      } catch (error) {
        this.$toast.fail('请检查手机号')
        return;
      }
      // 倒计时显示
      this.isCountDownShow = true;
      try {
        // 发送验证码请求
        await getPhoneSms(this.user.mobile)
        // 处理结果
        this.$toast.success('发送成功')
      } catch (error) {
       // 发送出错
        if(error.response.status===404){
          this.$toast.fail('手机号错误')
        }else if(error.response.status===429){
           this.$toast.fail('发送过于频繁，售后重试')
        }else{
          //发送按钮出来
          this.isCountDownShow = true;
          this.$toast.fail('发送出错，稍后重试')
          console.log(error);
        }
      }
    
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 162px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>